import unittest
from HTMLTestRunner.HTMLTestRunner import HTMLTestRunner
from email.mime.multipart import MIMEMultipart

# 任务一：继续开发减法，乘法，除法的用例

# 2.扫描刚才的4条用例
tests = unittest.defaultTestLoader.discover(r"D:\Users\北极星的眼泪\Desktop\demo\自动化测试第一二天",pattern="Test_Login.py")

# 3.执行并生成测试报告
# HTMLTestRunner

runner = HTMLTestRunner(
    title = "web自动化测试",
    description="财务理财系统web自动化测试",
    verbosity=2,# 详细程度
    stream = open(file="财务理财系统web自动化测试.html",mode="w+",encoding="utf-8")
)

runner.run(tests)


# 任务二：4.邮件发送代码，将刚才生成的测试报告做成附件发送到邮箱里

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
import base64

sender = '1272377298@qq.com'
receivers = ['1272377298@qq.com']  # 接收邮件，可设置为你的QQ邮箱或者其他邮箱


#汉字转base64
fromName64 = base64.b64encode(bytes('风晓', 'utf-8'))
#b'xxxx'转为'xxxx'
fromName64str = str(fromName64,'utf-8')
#尖括号拼接用双引号
fromNamestr = '"=?utf-8?B?' + fromName64str + '=?=" <' + "1272377298@qq.com" + ">"

# 创建一个带附件的实例
message = MIMEMultipart()
message['From'] = Header(fromNamestr)
message['To'] = Header("测试", 'utf-8')
subject = 'web自动化测试'
message['Subject'] = Header(subject, 'utf-8')

# 邮件正文内容
message.attach(MIMEText('财务理财系统web自动化测试', 'plain', 'utf-8'))

file = open("财务理财系统web自动化测试.html", 'rb')
f = file.read()
file.close()
# 构造附件1，传送当前目录下的 test.txt 文件
att1 = MIMEText(f, 'base64', 'gb2312')
att1["Content-Type"] = 'application/octet-stream'
# 这里的filename可以任意写，写什么名字，邮件中显示什么名字
att1.add_header("Content-Disposition","attachment",filename="财务理财系统web自动化测试.html")
# att1["Content-Disposition"] = 'attachment; filename="计算器.html"'
message.attach(att1)
try:
    smtpObj = smtplib.SMTP("smtp.qq.com")
    smtpObj.login("1272377298@qq.com", "bacbybgfwsapbagi")
    smtpObj.sendmail(fromNamestr, receivers, message.as_string())
    print("邮件发送成功")
except smtplib.SMTPException:
    print("Error: 无法发送邮件")
